package com.zhku.meeting.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhku.meeting.entities.Department;
import com.zhku.meeting.entities.Equipment;
import com.zhku.meeting.entities.Msg;
import com.zhku.meeting.mapper.EquipmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;

    public Msg getAll(Map<String, Object> para) {
        List<Equipment> equipment;
        Map<String,Object> map = new HashMap<>();
        if(para.containsKey("currentPage")){
            Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
            Integer pageSize = Integer.parseInt((String)para.get("pageSize"));
            PageHelper.startPage(currentPage,pageSize);
            equipment = equipmentMapper.selectAll();
            PageInfo pageInfo = new PageInfo(equipment);
            map.put("result",pageInfo);
            System.out.println("1");
        }else{
            equipment = equipmentMapper.selectAll();
            map.put("result",equipment);
            System.out.println("2");
        }
        return Msg.success().add(map);
    }

//    public Msg duplicateCheck(Map<String, Object> para) {
//        Integer equipId = (Integer) para.get("equipId");
//        String equipName = (String) para.get("equipName");
//        Equipment equipment = new Equipment(equipId,null,equipName,null);
//        List<Equipment> equipment1 = equipmentMapper.duplicateCheck(equipment);
//        if(equipment1!=null){
//            return Msg.fault().setError("设备名重复");
//        }else {
//            return Msg.success();
//        }
//    }

    public Msg update(Map<String, Object> para) {
        Integer equipId = (Integer) para.get("equipId");
        String roomNo = (String) para.get("roomNo");
        String equipName = (String) para.get("equipName");
        Boolean equipState = (Boolean) para.get("equipState");
        Equipment equipment = new Equipment(equipId,roomNo,equipName,equipState);
        int num = equipmentMapper.updateByPrimaryKey(equipment);
        return num>0?Msg.success():Msg.fault();
    }

    public Msg addEquip(Map<String, Object> para) {
        String roomNo = (String) para.get("roomNo");
        String equipName = (String) para.get("equipName");
//        Integer equipState = (Integer) para.get("equipState");
        Equipment equipment = new Equipment(null,roomNo,equipName,null);
        int num = equipmentMapper.insert(equipment);
        return num>0?Msg.success():Msg.fault();
    }

    public Msg deleteById(Map<String, Object> para) {
        Integer equipId = (Integer) para.get("equipId");
        int num = equipmentMapper.deleteByPrimaryKey(equipId);
        return num>0?Msg.success():Msg.fault();
    }

    public Msg selectCondition(Map<String, Object> para) {
        Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
        Integer pageSize = Integer.parseInt((String)para.get("pageSize"));
        Integer equipId = (Integer)para.get("equipId");
        String roomNo = null;
        String equipName = null;
        Boolean equipState = null;
        if(para.containsKey("roomNo")){
            roomNo = "%"+ para.get("roomNo")+"%";
        }
        if (para.containsKey("equipName")){
            equipName = "%"+para.get("equipName")+"%";
        }
        if (para.containsKey("equipState")){
            equipState = (Boolean) para.get("equipState");
        }
        Equipment equipment = new Equipment(equipId,roomNo,equipName,equipState);
        PageHelper.startPage(currentPage,pageSize);
        List<Equipment> equipment1 = equipmentMapper.selectByCondition(equipment);
        PageInfo pageInfo = new PageInfo(equipment1);
        Map<String,Object> map = new HashMap<>();
        map.put("result",pageInfo);
        return equipment1!=null?Msg.success().add(map):Msg.fault();
    }

    public Msg getEquip(Map<String, Object> para) {
        Integer id = Integer.parseInt((String)para.get("equipId")) ;
        Department department = equipmentMapper.selectByPrimaryKey(id);
        Map<String ,Object> map = new HashMap<>();
        map.put("result",department);
        return department!=null?Msg.success().add(map):Msg.fault();
    }
}
